import QtQuick 2.0
import org.qtproject.qtquick 1.0

import "qrc:/js/resources/Constants.js" as Constant

import "../common"
import "../page"


Item {
    id: id_friendList

    width: parent.width;
    height: m_heightItem

    property int m_margin: 10 * m_ratio

    property int m_widthImage: m_heightItem - 2 * m_margin;
    property int m_heighImage: m_heightItem - 2 * m_margin;

    property var m_uid;   // kieu long
    property var m_fid: buddyId;
    property string m_displayName: displayName;
    property string m_avatarUrl: avatarUrl;
    property string m_messageStatus: messageStatus;
    property string m_avatarUrlLocal : avatarUrlLocal;
    property string m_postTime: time;
    property bool visibleTime: typeMsg === 1 ? true : false
    property bool m_online: false

    property string onlineImg: online === -1 ? "qrc:/images/resources/offline-status-icon.png" : online == 1 ? "qrc:/images/resources/online-web-icon.png" :  "qrc:/images/resources/online-mobile-icon.png"

    property int m_totalMember: totalMember

signal heightItemChange(int heightItem);
onHeightChanged: {
    heightItemChange(id_friendList.height)
}

opacity: 1

Rectangle{
    anchors.fill: id_friendList
    color: "#ffffff"
}

Line {
    color: "#000000"
    width: parent.width - itemImage.width - m_margin
    x: itemImage.x + itemImage.width
}

Row {
    anchors.topMargin: m_margin
    anchors.leftMargin: m_margin
    anchors.bottomMargin: m_margin
    anchors.rightMargin: m_margin

    id: id_rowItem
    anchors.fill: id_friendList
    spacing: m_margin

    Item {
        id: itemImage
        width: m_widthImage
        height: m_heighImage
        anchors.verticalCenter: parent.verticalCenter

        Image {
            id: id_avatarUrl


            source: {
                if(m_avatarUrlLocal ===""){
                    return m_avatarUrl
                }else{
                    return "file://"+m_avatarUrlLocal;
                }


            }

            height: m_widthImage
            width: m_heighImage

            visible: false

            onStatusChanged: {
                if (id_avatarUrl.status == Image.Ready) {
                    id_avatarUrl.visible = true;
                    id_waitingImage.visible = false;
                }
            }
        }

        WaitingImage {
            id: id_waitingImage
            width: m_widthImage
            height: m_heighImage

            visible: true;
        }

        ImageTranparent {
            m_width: m_widthImage
            m_height: m_heighImage
            color: "transparent"
        }

        Image {
            id: id_imageOnOff
            //source: m_online === false? "qrc:/images/resources/offline-status-icon.png" : "qrc:/images/resources/online-mobile-icon.png"
            source: onlineImg

            width: 20 * m_ratio
            height: 20 * m_ratio

            x: 0.7 * parent.width
            y: 0.7 * parent.height
        }
    }


    Item {
        //spacing: m_margin
        x: itemImage.width
        width: parent.width - itemImage.width
        height: parent.height

        Row {
            id: id_rowDisplayName
            width: parent.width
            Text {
                id: id_txtDisplayName
                text: m_displayName
                width: parent.width - id_txtTime.contentWidth - m_margin
                font.family: "Arial"
                horizontalAlignment: Text.AlignJustify
                wrapMode: Text.WordWrap
                font.pixelSize: 20 * m_ratio;
            }

            Text {
                id: id_txtTime
                text: m_postTime

                visible: visibleTime

                font.family: "Arial"
                horizontalAlignment: Text.AlignJustify
                wrapMode: Text.WordWrap
                font.pixelSize: 14 * m_ratio;
                x: parent.width - id_txtTime.contentWidth - m_margin

                anchors.bottom: parent.bottom
            }
        }
        Row {
            anchors.top: id_rowDisplayName.bottom
            width: parent.width
            Text {
                id: id_txtMessageStatus
                text: m_messageStatus
                width: parent.width - 3 * m_margin
                font.family: "Arial"
                font.italic: true
                verticalAlignment: Text.AlignBottom
                horizontalAlignment: Text.AlignJustify
                wrapMode: Text.WordWrap
                opacity: 0.5

                font.pixelSize: 18 * m_ratio
            }

        }
    }
}

MouseArea {
    anchors.fill: id_friendList
    onClicked: {
        ApplicationInfo.onUpdateContactData(m_fid, m_uid, m_totalMember);

        Constant.m_uidActive = m_uid;
        Constant.m_fidActive = m_fid;
        Constant.m_avatarUrlFriendActive = m_avatarUrlLocal === "" ? m_avatarUrl : "file://"+m_avatarUrlLocal;
        Constant.m_displayNameFriendActive = m_displayName;


        id_pageView.pop();
        id_pageView.push(Qt.resolvedUrl("qrc:/Qml/gui/page/ChatPage.qml"));
        console.log("FriendListDelegate.qml=========== friend id: " + m_fid + " uid : "+ m_uid);
    }
}
}
